package cn.itcast.bos.web.action.take_delivery;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.service.take_delivery.OrderService;
import cn.itcast.bos.web.action.common.BaseAction;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
@Results({@Result(name="list",type="redirect",location="pages/take_delivery/dispatcher.jsp")
})
public class OrderAction extends BaseAction<Order> {
	@Autowired
	private OrderService orderService;
	@Action("orderAction_pageQuery")
	public String pageQuery() throws IOException{
		List<Order> list=orderService.findOrderByCourierId();
		Map<String, Object> map=new HashMap<>();
		map.put("total", list.size());
		map.put("rows", list);
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.setExcludes(new String[]{"workBills","subareas"});
		String json=JSONObject.fromObject(map,jsonConfig).toString();
		System.err.println(json);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		return null;
	}
	private String  pickerName;
	public void setPickerName(String pickerName) {
		this.pickerName = pickerName;
	}
	private Integer courierId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}


	@Action("orderAction_update")
	public String update(){
		String[] strings = pickerName.split("/");
		String sendAddress=strings[0]+strings[1]+strings[2]+model.getSendAddress();
		model.setSendAddress(sendAddress);
		orderService.updateOrderById(sendAddress,courierId,model.getId());
		return "list";
	}
}
